package com.flash.DayProcessing.controller;

import com.flash.BusinessParameters.service.SecurityService;
import com.flash.DayProcessing.entity.InventoryStatistics;
import com.flash.DayProcessing.entity.GoodName;
import com.flash.DayProcessing.service.InventoryService;
import com.flash.GlobalManagement.util.*;
import com.flash.InventoryManagement.entity.*;
import com.flash.InventoryManagement.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * 这是库存统计的控制层
 * @author 刘曦
 * @since 2022-8-22
 * @version 1.0
 */
@RestController
public class InventoryStatisticsController {
    @Autowired
    InventoryService inventoryService;
    @Resource
    CashStockService cashStockService;
    @Resource
    SecurityAccrualStockService securityAccrualStockService;
    @Resource
    SecurityStockService securityStockService;
    @Resource
    TaStockService taStockService;
    @Resource
    CashAccrualStockService cashAccrualStockService;

    /**
     * 查询网页数据的方法
     * @param ids 返回选择字符串
     * @param dateTime 选择的时间
     * @param request 获得session的参数
     * @return 返回的数据
     * @throws ParseException
     */
    @RequestMapping("selectInventory")
    public Map selectInventory(String ids,String dateTime,HttpServletRequest request) throws ParseException {
        //获得request中的session
        HttpSession session = request.getSession();
        //获得session中的fundId强转为String
        String  fundId = (String) session.getAttribute("fundId");
        //新建一个map
        HashMap<Object, Object> map = new HashMap<>();
        //新建一个list
        ArrayList<InventoryStatistics> list = new ArrayList<>();
        //往集合中存储数据
        for (int i = 0; i < 5; i++) {
            InventoryStatistics is = new InventoryStatistics();
            is.setFundId(fundId);
            is.setInventoryOperator("admin");
            is.setId(i+1);
            is.setInventoryStatis("未统计");
            list.add(is);
        }
        String  dateTimeTwo = null;
        list.get(0).setInventoryName("现金库存");
        list.get(1).setInventoryName("证券库存");
        list.get(2).setInventoryName("TA库存");
        list.get(3).setInventoryName("证券应收应付库存");
        list.get(4).setInventoryName("现金应收应付库存");
        Date dt = null;
        if (dateTime != null) {
            dateTime = dateTime.trim();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = df.parse(dateTime);
            Calendar instance = Calendar.getInstance();
            instance.setTime(parse);
            instance.add(Calendar.DATE, -1); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);
            dateTimeTwo=dateTimeTwo.trim();
            instance.setTime(time);
            instance.add(Calendar.DATE, +2); //得到前一天
            Date time2 = instance.getTime();
            dt = time2;
        }
        //判断字符串是否为空
        if (ids !=null && !ids.equals("")) {
            String[] id = null;
            //字符串分割
            id = ids.split(",");
            //循环判断数组中的数据
            for (String idd:id) {
                switch (idd){
                    case "1":
                        inventoryService.delete("cashStock",fundId,"dateCashStock",dateTime);
                        String dateCashStock = dateTimeTwo;
                        String businesstime = dateTime;
                        List<CashStock> cashStocks = inventoryService.cashStockAndFundsAllot(dateCashStock, businesstime);
                        for (int i = 0; i < cashStocks.size(); i++) {
                            CashStock cashStock = cashStocks.get(i);
                            cashStock.setCashStockId(NumberUtil.getNumber(TableIdUtil.CSID,TableNameUtil.CS,"CS"));
                            cashStock.setDateCashStock(businesstime);
                            cashStock.setInitialState(1);
                            cashStock.setRemark("");
                            cashStockService.insertCashStock(cashStock);
                        }
                        list.get(0).setInventoryDate(dt);
                        list.get(0).setInventoryData(cashStocks.size());
                        list.get(0).setInventoryStatis("已统计");
                        break;
                    case "2":
                        String dealTime = dateTime;
                        String dateStock = dateTimeTwo;
                        dateStock.trim();
                        dealTime.trim();
                        inventoryService.delete("securitystock",fundId,"dateStock",dateTime);
                        List<SecurityStock> securityStocks = inventoryService.securityStockAndDealData(dealTime, dateStock);
                        for (int i = 0; i < securityStocks.size(); i++) {
                            SecurityStock securityStock = securityStocks.get(i);
                            securityStock.setSecurityStockId(NumberUtil.getNumber(TableIdUtil.SSID,TableNameUtil.SS,"SS"));
                            securityStock.setDatestock(dateTime);
                            securityStock.setInitialState(1);
                            securityStock.setRemark("");
                            securityStockService.insertSecurityStock(securityStock);
                        }
                        list.get(1).setInventoryDate(dt);
                        list.get(1).setInventoryData(securityStocks.size());
                        list.get(1).setInventoryStatis("已统计");
                        break;
                    case "3":
                        inventoryService.delete("TaStock",fundId,"dateTAStock",dateTime);
                        String dateTAStock = dateTimeTwo;
                        dealTime = dateTime;
                        List<TaStock> taStocks = inventoryService.taStockAndTA(dateTAStock, dealTime);
                        for (int i = 0; i < taStocks.size(); i++) {
                            TaStock taStock = taStocks.get(i);
                            taStock.setSecurityAccrualStockId(NumberUtil.getNumber(TableIdUtil.TASID,TableNameUtil.TAS,"SAS"));
                            taStock.setDateTAStock(dealTime);
                            taStock.setInitialState(1);
                            taStock.setRemark("");
                            taStockService.insertTaStock(taStock);
                        }
                        list.get(2).setInventoryDate(dt);
                        list.get(2).setInventoryData(taStocks.size());
                        list.get(2).setInventoryStatis("已统计");
                        break;
                    case "4":
                        inventoryService.delete("securityAccrualStock",fundId,"dateAccrualStock",dateTime);
                        String dateAccrualStock = dateTimeTwo;
                        String businessTime = dateTime;
                        List<SecurityAccrualStock> securityAccrualStocks = inventoryService.securityAccrualStockAndSecurityAccrued(dateAccrualStock, businessTime);
                        for (int i = 0; i < securityAccrualStocks.size(); i++) {
                            SecurityAccrualStock securityAccrualStock = securityAccrualStocks.get(i);
                            securityAccrualStock.setDateAccrualStock(businessTime);
                            securityAccrualStock.setInitialState(1);
                            securityAccrualStock.setRemark("");
                            securityAccrualStock.setSecurityType(3);
                            securityAccrualStock.setSecurityAccrualStockId(NumberUtil.getNumber(TableIdUtil.SASID,TableNameUtil.SAS,"SAS"));
                            securityAccrualStockService.insertSecurityAccrualStock(securityAccrualStock);
                        }
                        list.get(3).setInventoryDate(dt);
                        list.get(3).setInventoryData(securityAccrualStocks.size());
                        list.get(3).setInventoryStatis("已统计");
                        break;
                    case "5":
                        inventoryService.deletete("cashAccrualStock",fundId,"dateAccrualStock",dateTime);
                        businessTime = dateTime;
                        dateAccrualStock = dateTimeTwo;
                        List<CashAccrualStock> cashAccrualStocks = inventoryService.cashAccrualStockAndCashAccrued(businessTime, dateAccrualStock);
                        for (int i = 0; i < cashAccrualStocks.size(); i++) {
                            CashAccrualStock cashAccrualStock = cashAccrualStocks.get(i);
                            cashAccrualStock.setCashAccrualStockId(NumberUtil.getNumber(TableIdUtil.CASID,TableNameUtil.CAS,"CAS"));
                            cashAccrualStock.setDateAccrualStock(businessTime);
                            cashAccrualStock.setInitialState(1);
                            cashAccrualStockService.insertCashAccrualStock(cashAccrualStock);
                        }
                        list.get(4).setInventoryDate(dt);
                        list.get(4).setInventoryData(cashAccrualStocks.size());
                        list.get(4).setInventoryStatis("已统计");
                        break;
                }
            }
        }
        map.put("code","0");
        map.put("msg","");
        map.put("count",10);
        map.put("data",list);
        return map;
    }
}
